var express = require("express");
var router = express.Router();
const db = require("../db/index.js");
// 新增驾校车辆

router.post("/addCar", function (req, res, next) {
  const info = req.body
  console.log(info);
  const sql = 'select * from car where plate=?'
  db.query(sql,info.plate,function(err,results){
    console.log(results);
    if(err) res.send({code:0,message:err.message})
    if(results.length>0) res.send({code:0,message:'该车已入库,请勿重复添加'})
    const sql1 = `insert into car set ?`
    db.query(sql1,{
      plate:info.plate,
      model:info.model,
      color:info.color,
      img:info.img,
      status:'正常使用',
      coach:'暂未分配'
    },function(err,data){
      if(err) res.send({code:0,message:err.message})
      res.send({code:200,message:'添加成功！'})
    })
  })
   
});
// 查询新闻列表
router.get('/list',function(req,res,next){
    console.log(req.query);
    // 获取参数   当前页码   当前每页数据量
    // 获取参数   当前页码   当前每页数据量
    let pageNumber = req.query.pageNumber
    // let pageSize = parseInt(req.body.pageSize)
    let pagesize = req.query.pagesize - 0
   // 求得sql语句中的参数
   console.log(pageNumber,pagesize);
  //  if(!pageNumber) pageNumber=1
  //  if(!pagesize) pagesize=6
   console.log(pageNumber,pagesize);
    let start = (pageNumber - 1) * pagesize
    // sql语句
    let sql = "select * from news limit ?, ?"
    let sqlParam = [start, pagesize]
    let sqlCount = "select count(*) from news"
  
    var count = 0
    // 执行查询
   db.query(sqlCount, null, (err, result) => {
     // 得到总记录数
     count = result[0]["count(*)"]
    
   })
  
   db.query(sql, sqlParam, (err, result) => {
     if (err) {
       res.send({
         code:0,
         message: err.message,
         ok:true
       })
     } else {
       res.send({
         code:200,
         message: "请求成功",
         ok: true,
         totalCount: count,
         data:result,
         
       })
     }
   })
     
  })
//   查询新闻详情
router.get('/getInfo',function(req,res,next){
    console.log(req.query);
    let id = req.query.id
    const sql = `select * from news where id=?`
    db.query(sql,id,function(err,result){
      if(err) {return res.send({code:0,message:err.message})}else{
        return res.send({code:200,message:'查询成功！',data:result[0]})
      }
    })
     
  })

module.exports = router